package com.ada.base.security.controller;

import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.ada.base.common.PageResult;
import com.ada.base.security.dto.PasswordDto;
import com.ada.base.security.entity.User;
import com.ada.base.security.entity.UserRole;
import com.ada.base.security.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户")
@RestController
@RequestMapping("base/user")
public class BaseUserController extends ControllerResponse {
    private UserService userService;

    public BaseUserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping
    public ApiResponse<PageResult> userList(@RequestParam(required = false, defaultValue = "1") long pageNum, @RequestParam(required = false, defaultValue = "20") int pageSize) {
        if (pageNum <= 0)
            pageNum = 1L;
        if (pageSize <= 0)
            pageSize = 20;
        return ok(userService.userList(pageNum, pageSize));
    }

//    @PostMapping("alloc")
    public ApiResponse<Object> allocRole(@RequestBody List<UserRole> userRoles) {
        userService.allocRole(userRoles);
        return ok();
    }

    @PostMapping("alloc/{type}")
    public ApiResponse<Object> allocRole(@RequestBody List<UserRole> userRoles, @PathVariable("type") int type) {
        userService.allocRole(userRoles, type);
        return ok();
    }

    @PostMapping
    public ApiResponse<Integer> add(@RequestBody User user) {
        return ok(userService.add(user));
    }

    @PutMapping("/mp")
    public ApiResponse<Boolean> modifyPassword(@Validated @RequestBody PasswordDto passwordDto) {
        return ok(userService.modifyPassword(passwordDto));
    }

    @GetMapping("/{code}")
    public ApiResponse<List<User>> userList(@PathVariable String code) {
       return ok(userService.queryByAuthority(code));
    }
}
